package com.aybived;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import java.util.Currency;

import android.content.Intent;
import android.content.res.Resources;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.Handler;
import android.os.Message;
import android.view.SurfaceHolder;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

import com.interfaces.FullscreenActivity;
import com.view.Scene;

public class main extends FullscreenActivity implements Runnable {
	
	private Button startGameButton;
	public static Resources resource;
	private PowerManager.WakeLock wakelock;
	public static main ref;
	private Thread myThread;
	private Boolean stopThread = false;
	private long timeBefore = 0;
	static private WindowManager windowManager;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ref = this; // save a reference
        windowManager=getWindowManager();
        resource = getResources();

        // display menu
//        setContentView(R.layout.main);
        
        // start game
        SceneHolder.getScene().reset_level();
		setContentView(SceneHolder.getScene());
		stopThread = false;
		myThread = new Thread(main.ref);
		myThread.start();
        
        // listen to click events
//        startGameButton = (Button)findViewById(R.id.button_start_game);
//        startGameButton.setOnClickListener(clickListener);
        PowerManager pm = (PowerManager)getSystemService( POWER_SERVICE );
        wakelock = pm.newWakeLock( PowerManager.FULL_WAKE_LOCK, "DisableScreenDimming" );
        
    }
    
    // click listener
    private OnClickListener clickListener = new OnClickListener() {
		public void onClick(View v) {
			if(v == startGameButton) {
				// start the game and switch view
				SceneHolder.getScene().reset_level();
				setContentView(SceneHolder.getScene());
				stopThread = false;
				myThread = new Thread(main.ref);
				myThread.start();
			}
		}
    };
    
    @Override
    protected void onStart() {
    	// TODO Auto-generated method stub
    	super.onStart();
    	wakelock.acquire();
    }
    @Override
    protected void onStop() {
    	// TODO Auto-generated method stub
    	super.onStop();
    	wakelock.release();
    	myThread.stop();
    }
    @Override
    protected void onResume() {
    	// TODO Auto-generated method stub
    	super.onResume();
    	wakelock.acquire();
    }

    public void endGame() {
    	stopThread = true;
    }
    
    private void showEndDialog() {
    	Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("You ran into a Wall!");
        builder.setMessage("Score: "+(int)SceneHolder.getScene().score+"\nPlay again?");
        builder.setPositiveButton("Yes!", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {
            	SceneHolder.getScene().resume();
            }
        });
        builder.setNegativeButton("No i'm frustrated!!", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {
            	main.ref.finish();
            }
        });
        builder.show();
    }
    
    public Handler updateHandler = new Handler(){
    	/** Gets called on every message that is received */
        // @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            // set view back to menu
            if(msg.what == 0) {
	            setContentView(R.layout.main);
	            startGameButton = (Button)findViewById(R.id.button_start_game);
	            startGameButton.setOnClickListener(clickListener);
            }
            if(msg.what == 1) {
            	showEndDialog();
            	//endGame();
            }
        }
    };
    
	public void run() {
		timeBefore = System.currentTimeMillis();
		while(true) {
			if(stopThread) {
				// dispatch message to main activity so that it displays the menu
				updateHandler.sendEmptyMessage(0);
				break;
			}
			double dt = (double)(System.currentTimeMillis() - timeBefore) / 1000.0f;
			timeBefore = System.currentTimeMillis();

			// update scene with dt
			SceneHolder.getScene().update(dt);
		}
	}

	static public int[] getScreenDimesions()
	{
		return new int[] { windowManager.getDefaultDisplay().getWidth(), windowManager.getDefaultDisplay().getHeight() };
	}

}